//
//  AddBankController.swift
//  Order
//
//  Created by Lazyloading on 2020/7/9.
//  Copyright © 2020 lazyloading. All rights reserved.
//

import UIKit
import PKHUD

class AddBankController: UIViewController{

    @IBOutlet weak var bankNameField: UITextField!
    @IBOutlet weak var usernameField: UITextField!
    @IBOutlet weak var numberField: UITextField!
    @IBOutlet weak var finishBtn: UIButton!
    
    @IBOutlet weak var topConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.loadDefaultSetting();
        self.initSubViews();
    }
    
    // MARK: 👉🏻 🐷Life cycle🐷
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews();
        self.topConstraint.constant = ISIPHONEX() ? 106 :  66;
    }
    // MARK: 👉🏻 🐷System Delegate🐷
    // MARK: 👉🏻 🐷Custom Delegate🐷
    // MARK: 👉🏻 🐷Event  Response🐷
    //添加
    @IBAction func finishAction(_ sender: Any) {
    
        let param = [
            "cardName" : self.usernameField.text ?? "",
            "cardNum" : self.numberField.text ?? ""
        ];
        
        MoyaRequest(.addBankCard(param), success: { (response : BaseResponse<[String : Any]>) in
            if response.respCode == 200 {
                HUD.flash(.label("添加成功"), delay: 1);
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self.navigationController?.popViewController(animated: true);
                }
            }else{
                HUD.flash(.label("添加失败：\(response.respMessage ?? "")"), delay: 1);
            }
        }) { (error) in
            HUD.flash(.label("添加失败"), delay: 1);
        };
        

    }
    @objc func textfieldValueChanged(textfield : UITextField) {
        ZMLog(textfield.text);
        
        if self.usernameField.text!.count  > 0 && self.numberField.text!.count > 0 && self.bankNameField.text!.count > 0 {
            self.finishBtn.isUserInteractionEnabled = true;
            self.finishBtn.backgroundColor = ZMColor.Orange.orange1;
        }else{
            self.finishBtn.isUserInteractionEnabled = false;
            self.finishBtn.backgroundColor = ZMHexColor("e4e4e4")
        }
        
    }
    // MARK: 👉🏻 🐷Private Methods🐷
    // MARK: 👉🏻 🐷Lazy loading🐷
    // MARK: 👉🏻 🐷Init SubViews🐷
    func loadDefaultSetting()  {
        
        self.edgesForExtendedLayout = .all;
        self.view.backgroundColor = .white;
        self.finishBtn.layer.cornerRadius = 10;
        self.finishBtn.layer.masksToBounds = true;
        self.finishBtn.backgroundColor = ZMHexColor("e4e4e4");
        self.usernameField.qmui_borderPosition = .bottom;
        self.numberField.qmui_borderPosition = .bottom;
        self.bankNameField.qmui_borderPosition = .bottom;
        self.usernameField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
        self.numberField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
        self.bankNameField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
    }
    
    func initSubViews()  {
        
    }

}
